package com.chen.facemanager.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayObject;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.chen.facemanager.bean.Sca;
import com.chen.facemanager.bean.WxOrder;
import com.chen.facemanager.conf.AlipayConfig;
import com.chen.facemanager.dao.ScaDao;
import com.chen.facemanager.dao.WxOrderDao;
import com.chen.facemanager.service.ALiservice;
import jdk.internal.util.EnvUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@Service
public class ALiserviceImpl implements ALiservice {
    private static final Logger logger = LoggerFactory.getLogger("MainLogger");

    @Autowired
    private WxOrderDao wxOrderDao;

    @Autowired
    private ScaDao scaDao;

    @Value("${CHEN.GETWAY}")
    private String GETWAY;

    @Autowired
    private AlipayConfig alipayConfig;
    /**
     * app支付接口2.0
     */

    @Override
    public Map pay(String orderId,String uid,String hid,String totalFee,String htype) throws Exception {
        try {
            System.out.println("-----------------------------支付宝预支付开始------------------------------------");
            // 封装请求客户端  实例化客户端  这个顺序不要动
            AlipayClient client = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
                    alipayConfig.getApp_id(),
                    alipayConfig.getMerchant_private_key(),
                    alipayConfig.getFormat(),
                    alipayConfig.getCharset(),
                    alipayConfig.getAlipay_public_key(),
                    alipayConfig.getSignType());
            //	支付请求 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            AlipayTradeAppPayRequest payRequest = new AlipayTradeAppPayRequest();
            // SDK已经封装掉了公共参数，这里只需要传入业务参数。

            AlipayTradeAppPayModel appPayModel = new AlipayTradeAppPayModel();
            appPayModel.setSubject("身份验证费用");
            appPayModel.setOutTradeNo(orderId);
            appPayModel.setTotalAmount(totalFee);        //设置总金额
            appPayModel.setTimeoutExpress("90m");         //设置超时关闭该订单时间
            appPayModel.setProductCode("QUICK_MSECURITY_PAY");//销售产品码
            String attach = "a,"+uid+","+hid+","+htype;
            appPayModel.setPassbackParams(URLEncoder.encode(attach));
            payRequest.setBizModel(appPayModel);

            payRequest.setNotifyUrl(alipayConfig.getNotify_url());//设置异步通知地址
            //支付宝请求应答
            AlipayTradeAppPayResponse responses = client.sdkExecute(payRequest);
            if (responses != null){
                // 写入订单数据
                wxOrderDao.save(new WxOrder(orderId,responses.getTradeNo(),hid,uid,(System.currentTimeMillis() / 1000),Integer.parseInt(totalFee) * 100,100,2,htype));
            }
            System.out.println("-----------------------------支付宝应答处理开始------------------------------------");
            System.out.println(responses.getBody()); // 就是orderString 可以直接给客户端请求，无需再做处理
            Map<String, String> data = new HashMap<>();
            data.put("body",responses.getBody());   //支付表单
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new Exception("下单失败");
    }

    /**
     *支付回调
     * @param request
     * @param response
     */
    @Override
    @Transactional
    public void paymentCallback(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("---------------------------支付宝进入异步通知--------------------------");
        String resultFail = "fail";
        //获取支付宝GET过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        System.out.println("返回的map----------------" + requestParams);
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            /*try {
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }*/
            params.put(name, valueStr);
        }

        boolean signVerified = false; //调用SDK验证签名
        try {
            signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipay_public_key(), alipayConfig.getCharset(), alipayConfig.getSignType());
        } catch (AlipayApiException e) {
            logger.error("【支付宝异步通知】支付宝回调通知失败 e={} params={}", e, params);
            responseBody(response, resultFail);
            return;
        }
        if (!signVerified) {
            logger.error("【支付宝异步通知】验证签名错误 params={} ", params);
            responseBody(response, resultFail);
            return;
        }
        BigDecimal trade_price = new BigDecimal(request.getParameter("total_amount"));
        //商户订单号
        String out_trade_no = params.get("out_trade_no");
        //支付宝交易号
        String trade_no = params.get("trade_no");
        //交易状态
        String trade_status = params.get("trade_status");
        // 支付成功修改订单状态
        if (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) {
            //业务处理，主要是更新订单状态
            System.out.println("---------------------------支付成功--------------------------");
            String passback_params = params.get("passback_params");
            String attach = null;
            try {
                attach = URLDecoder.decode(passback_params,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (attach != "" || attach != null){
                if (attach.substring(0,1).equals("a")){
                    String[] att = attach.split(",");
                    Sca s = new Sca(att[1],att[2],att[3],0,"","");
                    System.out.println("身份核验支付成功回调附加数据："+s.toString());
                    scaDao.save(s);
                }
                if (attach.substring(0,1).equals("b")){
                    String[] att = attach.split(",");
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", att[1]);
                    map.put("idnumber", att[2]);
                    map.put("terminalid", att[3]);
                    map.put("uid", att[4]);

                    JSONObject jsonObject = JSONUtil.parseObj(map);

                    String s = HttpUtil.post(GETWAY+"service/jsz",jsonObject.toString());
                    System.out.println("驾驶证核验支付成功回调附加数据："+s.toString());
                }
            }

            wxOrderDao.updateByOrderid(out_trade_no);


            responseBody(response, "true");
            return;
        }
        responseBody(response, resultFail);
        return;
    }
    private void responseBody(HttpServletResponse response, String contentBody) {
        try {
            response.setContentType("type=text/javascript;charset=UTF-8");
            String s = contentBody;
            response.getWriter().write(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }
}
